package initilization

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	beego "github.com/beego/beego/v2/server/web"
	_ "github.com/go-sql-driver/mysql"
	"ksd-social-api/commons/global"
	amodel "ksd-social-api/modules/advert/model"
	hmodel "ksd-social-api/modules/channel/model"
	mmodel "ksd-social-api/modules/msg/model"
	ntmodel "ksd-social-api/modules/note/model"
	pmodel "ksd-social-api/modules/platform/model"
	rtmodel "ksd-social-api/modules/resume/model"
	zmodel "ksd-social-api/modules/searchkeywords/model"
	spmodel "ksd-social-api/modules/special/model"
	umodel "ksd-social-api/modules/user/model"
	"ksd-social-api/utils"
	"time"
)

// 数据库连接工作
func InitMYSQL() {
	// spmodel
	if global.Env == "dev" {
		orm.Debug = true
	}

	// 用户模型
	orm.RegisterModel(&umodel.User{})
	orm.RegisterModel(&umodel.UserBenefits{})
	orm.RegisterModel(&umodel.UserVip{})
	orm.RegisterModel(&umodel.UserBuyVip{})
	orm.RegisterModel(&umodel.UserBuyBbs{})
	orm.RegisterModel(&umodel.UserFocus{})
	orm.RegisterModel(&umodel.UserWalletIncome{})
	orm.RegisterModel(&umodel.UserVipCode{})
	orm.RegisterModel(&umodel.UserWalletCode{})
	orm.RegisterModel(&umodel.UserWalletRecords{})
	// 消息模型
	orm.RegisterModel(&mmodel.MessagePoint{})
	orm.RegisterModel(&mmodel.MessageMe{})
	orm.RegisterModel(&mmodel.MessageSystem{})
	// 搜索关键词
	orm.RegisterModel(&zmodel.SearchKeywords{})
	// 简历
	orm.RegisterModel(&rtmodel.Resume{})
	orm.RegisterModel(&rtmodel.ResumeCode{})
	orm.RegisterModel(&rtmodel.ResumeTemplate{})
	orm.RegisterModel(&rtmodel.ResumeTemplateCode{})
	orm.RegisterModel(&rtmodel.UserBuyResumeTemplate{})
	// 笔记模型
	orm.RegisterModel(&ntmodel.Note{})
	orm.RegisterModel(&ntmodel.UserBuyNote{})
	orm.RegisterModel(&ntmodel.NoteCategory{})
	orm.RegisterModel(&ntmodel.NoteComment{})
	orm.RegisterModel(&ntmodel.NoteCommentReply{})
	orm.RegisterModel(&ntmodel.NoteUserLike{})
	orm.RegisterModel(&ntmodel.NoteUserFav{})
	orm.RegisterModel(&ntmodel.NoteCode{})
	orm.RegisterModel(&ntmodel.NoteUserHits{})
	orm.RegisterModel(&ntmodel.NoteChapter{})
	orm.RegisterModel(&ntmodel.NoteLesson{})
	orm.RegisterModel(&ntmodel.NoteStudyUserRecords{})
	// 专题模型
	orm.RegisterModel(&spmodel.Special{})
	orm.RegisterModel(&spmodel.UserBuySpecial{})
	orm.RegisterModel(&spmodel.SpecialCategory{})
	orm.RegisterModel(&spmodel.SpecialComment{})
	orm.RegisterModel(&spmodel.SpecialCommentReply{})
	orm.RegisterModel(&spmodel.SpecialUserLike{})
	orm.RegisterModel(&spmodel.SpecialUserFav{})
	orm.RegisterModel(&spmodel.SpecialCode{})
	orm.RegisterModel(&spmodel.SpecialUserHits{})
	orm.RegisterModel(&spmodel.SpecialBindCode{})
	orm.RegisterModel(&spmodel.SpecialBind{})
	orm.RegisterModel(&spmodel.SpecialBindDetail{})
	orm.RegisterModel(&spmodel.SpecialBindUserBuy{})
	// 广告模型
	orm.RegisterModel(&amodel.Advert{})
	// 系统
	orm.RegisterModel(&pmodel.PlatformSystem{})
	// 栏目
	orm.RegisterModel(&hmodel.Channel{})
	orm.RegisterModel(&hmodel.ChannelCode{})
	orm.RegisterModel(&hmodel.UserBuyChannel{})
	// 获取自定义的数据库配置信息
	dbtype, _ := beego.AppConfig.String("db.type")
	dbuserame, _ := beego.AppConfig.String("db.username")
	dbpassword, _ := beego.AppConfig.String("db.password")
	dbhost, _ := beego.AppConfig.String("db.host")
	dbport, _ := beego.AppConfig.Int("db.port")
	dbname, _ := beego.AppConfig.String("db.name")
	dbparams, _ := beego.AppConfig.String("db.params")
	// 获取链接字符串
	mysqlConn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?%s", dbuserame, dbpassword, dbhost, dbport, dbname, dbparams)
	// 设置数去
	orm.DefaultTimeLoc = time.UTC
	// 注册驱动类型是MYSQL
	orm.RegisterDriver(dbtype, orm.DRMySQL)
	fmt.Println("mysqlConn", mysqlConn)
	// 连接数据库
	err := orm.RegisterDataBase("default", dbtype, mysqlConn)
	// 同步数据库表
	orm.RunSyncdb("default", false, true)
	if err != nil {
		global.SugarLog.Error("数据库MYSQL连接失败，失败的原因是：%v", err)
		return
	}

	global.SugarLog.Debug("数据库MYSQL连接成功!!!")
	//// 创建orm对象
	mysql := orm.NewOrm()
	var count int
	mysql.Raw("select count(1) from xk_user where username='feige'").QueryRow(&count)
	if count == 0 {
		uuid := utils.GetUUID()
		getUUID := utils.GetUUID()
		md5Password := utils.Md5Slat("123456", uuid)
		user := new(umodel.User)
		user.Id = 2
		user.Uuid = getUUID
		user.Username = "feige"
		user.Telephone = "15074816438"
		user.Slat = uuid
		user.Password = md5Password
		user.BirthDay = ""
		user.Avatar = "/avatar/07.png"
		user.BgImg = "/avatar/07.png"
		user.Male = 1
		mysql.Insert(user)
	}
}
